package com.fone.player.online.adapter;

import java.util.List;

import com.fone.player.R;
import com.fone.player.entity.SubjectInfo;
import com.fone.player.util.FoneUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LeaderboardAdapter extends BaseAdapter {
	
	/** 日志标签 */
//	private static final String TAG = "CommonAdapter";

	private String shost;
//	private ArrayList<Cnt> cntList;
	private List<SubjectInfo> subjectList;
	private LayoutInflater layoutInflater;
	
	private int[] picSourceId;
	DisplayImageOptions options;

	public LeaderboardAdapter(Context context, String shost, List<SubjectInfo> subjectList) {
		super();
		picSourceId = new int[]{R.drawable.sign_top1,R.drawable.sign_top2,
								R.drawable.sign_top3,R.drawable.sign_top4,
								R.drawable.sign_top5,R.drawable.sign_top6,
								R.drawable.sign_top7,R.drawable.sign_top8,
								R.drawable.sign_top9,R.drawable.sign_top10};
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.default_340_192)
		.showImageForEmptyUri(R.drawable.default_340_192)
		.showImageOnFail(R.drawable.default_340_192)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.build();
		
		layoutInflater = LayoutInflater.from(context);
		initData(shost, subjectList);
	}

	public void initData(String shost, List<SubjectInfo> subjectList) {
		this.shost = shost;
		this.subjectList = subjectList;
	}

	@Override
	public int getCount() {
		if(subjectList.size()-4<0){
			return subjectList.size();
		}
		return 4;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SubjectInfo info = subjectList.get(position); 

		ViewHolder vHolder = null;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.item_leaderboard_list, null, false);
			// 创建 ViewHodler 对象
			vHolder = new ViewHolder();
			vHolder.pic = (ImageView) convertView.findViewById(R.leaderboard.pic);
			vHolder.signPic = (ImageView) convertView.findViewById(R.leaderboard.item_sign);
			vHolder.playTimes = (TextView) convertView.findViewById(R.leaderboard.item_play_times);
			vHolder.title = (TextView) convertView.findViewById(R.leaderboard.name);
			vHolder.content = (TextView) convertView.findViewById(R.leaderboard.content);
			// 设置 Tag
			convertView.setTag(vHolder);
		} else {
			vHolder = (ViewHolder) convertView.getTag();
		}
		
		//TODO 设置数据
		
		//设置sign
		if(picSourceId.length>position){
			vHolder.signPic.setBackgroundResource(picSourceId[position]);
		}
		// 设置图片
		ImageLoader.getInstance().displayImage(FoneUtil.getAbsoluteUrl("", shost, info.pic2), vHolder.pic, options);
		// 设置title
		vHolder.title.setText(info.name);
		// 设置描述
		vHolder.content.setText(info.desc);
		//设置播放次数
		int showCount = FoneUtil.String2Integer(info.showcount);
		if(showCount>10000){
			vHolder.playTimes.setText(showCount/10000+"."+showCount/1000%10+"万次");
		}else{
			vHolder.playTimes.setText(showCount+"次");
		}
		
		return convertView;
	}

	static class ViewHolder {
		ImageView pic;//海报图片
		ImageView signPic;//排名图片
		TextView playTimes;//播放次数
		TextView title;//标题
		TextView content;//视频介绍
	}

	
}
